package com.company.bookstore.controller.book_module;


import com.company.bookstore.common.result.MvcResult;
import com.company.bookstore.entities.book_module.BookImg;
import com.company.bookstore.service.book_module.BookImgService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 姬深威
 * @since 2020-09-12
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/public/bookImg")
public class BookImgController {

    @Resource
    BookImgService bookImgService;


    @PostMapping("/addImgs/{bookId}")
    public MvcResult<Object> addImgs(@PathVariable Integer bookId, @RequestBody List<BookImg> imgs) {
        try {
            bookImgService.addImgs(bookId, imgs);
            return MvcResult.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return MvcResult.ADD_FAIL;
        }

    }

    @PostMapping("/deleteImg/{bookId}/{imgId}")
    public MvcResult<Object> deleteImg(@PathVariable Integer bookId, @PathVariable Integer imgId) {
        Integer affectRow = 0;
        affectRow = bookImgService.deleteImg(bookId, imgId);
        if (affectRow == 0) {
            return MvcResult.DELETE_FAIL;
        } else {
            return MvcResult.DELETE_SUCCESS;
        }
    }

    @PostMapping("/deleteImgList/{bookId}")
    public MvcResult<Object> deleteImgList(@PathVariable Integer bookId, @RequestBody  Integer[] imgIds) {
        Integer affectRow = 0;
        try {
           bookImgService.deleteImgList(bookId, imgIds);
            return MvcResult.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return MvcResult.DELETE_FAIL;
        }

    }


}

